package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 角色信息
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;

    @GetMapping("/list")
    public AjaxResult list() {
        List<SysMenu> menus = menuService.list();
        return AjaxResult.success(menus);
    }

    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(menuService.getById(menuId));
    }

    @PostMapping
    public AjaxResult add(@RequestBody SysMenu menu) {
        return toAjax(menuService.save(menu));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody SysMenu menu) {
        return toAjax(menuService.updateById(menu));
    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect()
    {
        return AjaxResult.success(menuService.buildMenuTreeSelect(0L));
    }

    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        long[] test = new long[1];
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", test);
        ajax.put("menus", menuService.buildMenuTreeSelect(roleId));
        return ajax;
    }

    protected AjaxResult toAjax(boolean status) {
        return status ? AjaxResult.success() : AjaxResult.error();
    }
}
